unit SelectBatch;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, BaseDialog, cxGraphics, cxLookAndFeels, cxLookAndFeelPainters,
  Menus, StdCtrls, cxButtons, ExtCtrls, cxControls, cxStyles, dxSkinsCore,
  dxSkinsDefaultPainters, dxSkinscxPCPainter,
  cxDataStorage, cxEdit, DB, cxDBData, cxGridLevel, cxClasses,
  cxGridCustomView, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxGrid, cxContainer, cxTextEdit,
  cxLabel, DBClient, Provider, DBAccess, Uni, dxSkinsForm,
  cxNavigator, cxCustomData, cxFilter, cxData, MemDS, dxDateRanges,
  cxDataControllerConditionalFormattingRulesManagerDialog,
  DSUtils, DataModule, MisString, GlobalVar;

type
  TfrmSelectBatch = class(TfrmBaseDialog)
    ViewBatch: TcxGridDBTableView;
    LevelBatch: TcxGridLevel;
    cxGridBatch: TcxGrid;
    dsBatch: TDataSource;
    qryBatch: TUniQuery;
    cxLabel1: TcxLabel;
    edtBatchFilter: TcxTextEdit;
    qryBatchbatchcode: TStringField;
    qryBatchquantity: TFloatField;
    qryBatchassqty: TFloatField;
    ViewBatchselected: TcxGridDBColumn;
    ViewBatchbatchcode: TcxGridDBColumn;
    ViewBatchquantity: TcxGridDBColumn;
    ViewBatchassqty: TcxGridDBColumn;
    btnAll: TcxButton;
    btnClear: TcxButton;
    qryBatchselected: TBooleanField;
    dspBatch: TDataSetProvider;
    cdsBatch: TClientDataSet;
    cdsBatchselected: TBooleanField;
    cdsBatchbatchcode: TStringField;
    cdsBatchquantity: TFloatField;
    cdsBatchassqty: TFloatField;
    qryBatchproductdate: TDateTimeField;
    qryBatchvaliddate: TDateTimeField;
    cdsBatchproductdate: TDateTimeField;
    cdsBatchvaliddate: TDateTimeField;
    ViewBatchProductDate: TcxGridDBColumn;
    ViewBatchValiddate: TcxGridDBColumn;
    procedure ViewBatchCellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure btnAllClick(Sender: TObject);
    procedure btnClearClick(Sender: TObject);
  private
    { Private declarations }
    sOldSQL: string;
    procedure OpenBatch(sFilter: string = '');
    procedure SetAllSelect(bSel: Boolean = true);
  public
    { Public declarations }
    FProductID: integer;
    FDepotID: integer;
    FUnitID: integer;
    constructor Create(AOwner: TComponent; DepotID, ProductID, UnitID: integer;
      bAdd: Boolean = False); reintroduce;
  end;

var
  frmSelectBatch: TfrmSelectBatch;

implementation

{$R *.dfm}

{ TfrmSelectBatch }

constructor TfrmSelectBatch.Create(AOwner: TComponent; DepotID, ProductID,
  UnitID: integer; bAdd: Boolean);

begin
  inherited Create(AOwner);
  FDepotID := DepotID;
  FProductID := ProductID;
  FUnitID := UnitID;
  OpenBatch();
  ViewBatch.GetColumnByFieldName('selected').Visible := bAdd;
  btnAll.Visible := bAdd;
  btnClear.Visible := bAdd;
end;

procedure TfrmSelectBatch.OpenBatch(sFilter: string = '');
var
  iDepotBatchDate: integer;
  Filter:string;
begin
  Filter :='';
  if (Trim(sFilter) <> '') then
    Filter := ' and BatchCode like ''%'' + sFilter + ''%''';
  sOldSQL := ' SELECT cast(0 as bit) as selected, b.batchcode,round(b.quantity/u.rate,4) as quantity'
    + '  ,b.assqty,b.productdate,b.validdate FROM tt_bookbatch b'
    + ' left join td_unitprice u on (b.productid=u.productid and u.u_no>0)'
    + '      WHERE b.depotid =:depotid AND b.productid =:productid'
    + '      and u.unitid=:UnitID'+Filter;
  iDepotBatchDate := StrToIntDef(GetSysParamValue('depotbatchdate'), 0);
  if iDepotBatchDate = 0 then
  begin
    sOldSQL := ' SELECT cast(0 as bit) as selected, b.batchcode,round(sum(b.quantity)/u.rate,4) as quantity'
      + '  ,sum(b.assqty) assqty,max(productdate) productdate,max(validdate) validdate FROM tt_bookbatch b'
      + ' left join td_unitprice u on (b.productid=u.productid and u.u_no>0)'
      + ' WHERE b.depotid =:depotid AND b.productid =:productid'
      + '   and u.unitid=:UnitID'+Filter
      + ' group by b.batchcode,u.rate';
  end;
  qryBatch.Close();
  qryBatch.SQL.Text := sOldSQL;
  qryBatch.Params.ParamByName('depotid').Value := FDepotID;
  qryBatch.Params.ParamByName('productid').Value := FProductID;
  qryBatch.Params.ParamByName('unitid').Value := FUnitID;
  cdsBatch.Open();
end;

procedure TfrmSelectBatch.ViewBatchCellDblClick(
  Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  inherited;
  if (not cdsBatch.IsEmpty()) then
    ModalResult := mrOk;
end;

procedure TfrmSelectBatch.btnAllClick(Sender: TObject);
begin
  inherited;
  SetAllSelect;
end;

procedure TfrmSelectBatch.SetAllSelect(bSel: Boolean);
begin
  cdsBatch.First;
  cdsBatch.DisableControls;
  while (not cdsBatch.Eof) do
  begin
    SetFieldVal(cdsBatch, 'selected', bSel);
    cdsBatch.Post;
    cdsBatch.Next;
  end;
  cdsBatch.EnableControls;
end;

procedure TfrmSelectBatch.btnClearClick(Sender: TObject);
begin
  inherited;
  SetAllSelect(False);
end;

end.
